package com.motorhome.manage.service.impl;

import com.motorhome.base.util.Result;
import com.motorhome.base.util.StringUtil;
import com.motorhome.manage.dao.UserDao;
import com.motorhome.manage.domain.ManageUser;
import com.motorhome.manage.service.MainService;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: John
 */
@Service
public class MainServiceImpl implements MainService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private UserDao userDao;

    @Override
    public Result savePass(ManageUser manageUser, String password, String password1, String password2) {

        if (StringUtil.isBlank(password)) {
            return new Result(1, "旧密码不能为空");
        }
        if (StringUtil.isBlank(password)) {
            return new Result(1, "新密码不能为空");
        }
        if (StringUtil.isBlank(password)) {
            return new Result(1, "确认密码不能为空");
        }
        if (!password1.equals(password2)) {
            return new Result(1, "两次新密码不一致");
        }

        ManageUser loginUser = userDao.queryUserById(manageUser.getId());
        if (!loginUser.getLoginPass().equals(DigestUtils.md5Hex(password))) {
            return new Result(1, "旧密码不正确");
        }
        int row = userDao.updatePass(loginUser.getId(), DigestUtils.md5Hex(password1));

        return new Result(0, "密码修改成功");
    }

}
